﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShiXuLong.Service;
using ShiXuLong.Model;

namespace ShiXuLong.Web.Controllers
{

    public class CartController : Controller
    {
        FrontDesk fd = new FrontDesk();
        UserService us = new UserService();
        // GET: Cart
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 购物车数据显示，购物车分布视图页面
        /// </summary>
        /// <returns></returns>

        /// <summary>
        /// 删除购物车中的一个商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DelCartShop(int id)
        {
            var res = fd.DelCartShop(id);
            if (res > 0)
            {
                return Json(new { msg = "执行成功", success = 1 });

            }
            else
            {
                return Json(new { msg = "执行失败", success = 0 });
            }

        }
        /// <summary>
        /// 清空购物车
        /// </summary>
        /// <returns></returns>
        public ActionResult ClearCart()
        {
            var res = fd.ClearCart();
            if (res > 0)
            {
                return Json(new { msg = "执行成功", success = res });
            }
            return Json(new { msg = "网络被外星人劫持了", success = res });

        }

        /// <summary>
        /// 更新购物车商品数量
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ShopNum"></param>
        /// <returns></returns>
        public ActionResult UpdateNum(int id, int ShopNum)
        {
            var res = fd.UpdateCartsNum(id, ShopNum);
            if (res > 0)
            {
                return Json(new { success = res, msg = "执行成功" });
            }
            else
            {
                return Json(new { success = res, msg = "执行成功" });
            }

        }
        /// <summary>
        /// 暂时无用
        /// </summary>
        /// <returns></returns>
        public ActionResult AddressView()
        {
            return View();
        }
        /// <summary>
        /// 结算页面，在这个页面要确认收货地址
        /// </summary>
        /// <returns></returns>
        public ActionResult PlaceOrderView(string ids)
        {
            if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
            }
            var lists = fd.GetCart(ids);
            var userId = MyConfig.UserID;
            ViewBag.AdminUrl = MyConfig.AdminUrl;
            ViewBag.Address = us.GetUserAddressById(userId);
            ViewBag.ListShop = fd.GetOrdersById(ids);
            return View(lists);
        }
        public ActionResult GetAddress(int id)
        {
            var list = fd.GetAddress(id);
            if (list==null)
            {
                return Json(new { success = false, data = 0 });
            }
            return Json(new { success = true, data = list.UserAddress }) ;
        }
        /// <summary>
        /// 下单
        /// </summary>
        /// <returns></returns>
        public ActionResult PlaceOrder(string cartIds, string Address)
        {

            var res = fd.PlaceOrder(cartIds, Address);
            if (res > 0)
            {
                return Json(new { msg = "下单成功", success = 1 });
            }
            else
            {
                return Json(new { msg = "执行失败", success = 0 });
            }
        }
        [HttpGet]
        public ActionResult AddUserAddressView()
        {

            var Address = new Address
            {
                UserId = MyConfig.UserID
            };
            return View(Address);
        }
        [HttpPost]
        public ActionResult AddUserAddress(Address address)
        {
            var res = us.AddUserAddress(address);
            if (res > 0)
            {
                return Json(new { msg = "用户地址添加成功", success = true });
            }
            else
            {
                return Json(new { msg = "用户地址添加失败", success = false });

            }

        }
    }
}